import React from 'react'
import { Form , Button, Input} from 'react-vant';
import {useSelector,useDispatch} from "react-redux"
import {useNavigate,useLocation} from "react-router-dom"
import {debounce} from "lodash"
import * as action from "../action"
import {useEffect} from "react"
function Form1() {
    const location = useLocation()
    const dispatch = useDispatch()
    const Navigate = useNavigate()
    const store = useSelector(state=>({...state.shopreducer}))
    const [form] = Form.useForm();
    console.log(form,111);
    const onFinish = (values) => {
      console.log(values);
      if (location.state.isedit === "编辑") {
          console.log(location.state.item,location.state.editindex,11111);

          dispatch(action.editaddress( Object.assign({},values,{editindex:location.state.editindex}) ))
      } else {
        dispatch(action.addaddress(values))
      }
     
      setTimeout(()=>{
        Navigate("/all/address")
      },100)
    };
    useEffect(()=>{
      console.log(location.state);
      if (location.state.isedit === "编辑") {
        console.log(location.state);
        form.setFieldsValue(location.state.item)
      }
    
    },[])
    return (
        <Form
          form={form}
          onFinish={onFinish}
          footer={
            <div style={{ margin: '16px 16px 0' }}>
              <Button round nativeType="submit" type="primary" block>
                提交
              </Button>
            </div>
          }
        >
          <Form.Item
            tooltip={{
              message:
                'A prime is a natural number greater than 1 that has no positive divisors other than 1 and itself.',
            }}
            intro="确保这是唯一的用户名"
            rules={[{ required: true, message: '请填写用户名' }]}
            name="name"
            label="性名"
          >
            <Input placeholder="请输入用户名" />
          </Form.Item>
          <Form.Item rules={[{ required: true, message: '请输入电话号码' }]} name="tel" label="电话">
            <Input placeholder="请输入密码" />
          </Form.Item>
          <Form.Item rules={[{ required: true, message: '请输入住址' }]} name="address" label="电话">
            <Input placeholder="请输入密码" />
          </Form.Item>
        </Form>
      );
}

export default Form1